package com.mezizy.ifaner;

import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.mezizy.ifaner.global.Global;
import com.mezizy.ifaner.global.com.mezizy.ifaner.web.data.Dish;
import com.mezizy.ifaner.global.com.mezizy.ifaner.web.data.Order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class OrderDetailActivity extends ActionBarActivity {
    Order order;
    LinearLayout linearOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_order_detail);

        getSupportActionBar().hide();
        order = (Order) getIntent().getSerializableExtra("order");

        String orderId = getIntent().getStringExtra("orderId");
        if (orderId==null){
            finish();
        }

        Bitmap bitmap = null;
        try {
            bitmap = Global.createQRCode(orderId, 200);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        //TODO 如果已经完成不显示 QR 和 单号
        if (orderId.equals("0")){
            ((RelativeLayout) (findViewById(R.id.imgOrderQrCode)).getParent()).removeView(findViewById(R.id.imgOrderQrCode));
            ((RelativeLayout) (findViewById(R.id.txtOrderId)).getParent()).removeView(findViewById(R.id.txtOrderId));


        }else{
            ((ImageView) findViewById(R.id.imgOrderQrCode)).setImageBitmap(bitmap);
            ((TextView) findViewById(R.id.txtOrderId)).setText("訂單密碼："+orderId);
        }


        ((TextView) findViewById(R.id.txtTotalPrice)).setText("NT " + order.getAmount());
        ((TextView) findViewById(R.id.txtAddress)).setText(order.getDeliverPlace());
        ((TextView) findViewById(R.id.txtOrderDate)).setText(order.getCreateTime());
        ((TextView) findViewById(R.id.txtRemark)).setText(order.getRemarks() );

        ((TextView) findViewById(R.id.txtStoreName)).setText(order.getMerchantName());


        linearOrder=(LinearLayout) findViewById(R.id.linearOrders);
        orderAdapter(linearOrder, orderId);

    }

    public void btnOrderDetailCloseClick(View v) {
        finish();
    }
    @Override
    protected void onStart() {
        super.onStart();
        Global.activityLogInCheck(this);

    }
    public List<Map<String, Object>> getDataOrder(String orderId) {
        List<Map<String, Object>> list = new ArrayList<>();
        Order order = Global.orderMap.get(orderId);
        if (order == null) {
            return list;
        }
        List<Dish> listDish = order.getDishList();
        for (Dish dish : listDish) {
            Map<String, Object> map = new HashMap<>();
            //TODO * 修改成直接得到Name
            map.put("txtOrderListDishName", dish.getDishesName());
            //TODO 修改Quantity
            map.put("txtOrderListDishQuantity", "×" + dish.getCartQuantity());
            map.put("txtOrderListDishPrice", "NT " + dish.getTotal());
             list.add(map);
        }


        return list;
    }
    @Override
    protected void onStop() {
        super.onStop();
        Global.dismissProgress(this);
    }
    private LinearLayout orderAdapter(LinearLayout parent, String orderId) {

        List<Map<String, Object>> dataOrder = getDataOrder(orderId);


        TextView txtOrderListDishName, txtOrderListDishQuantity, txtOrderListDishPrice;


        parent.removeAllViews();

        for (Map<String, Object> stringObjectMap : dataOrder) {
            View child = View.inflate(this, R.layout.list_order_list, null);
            txtOrderListDishName = (TextView) child.findViewById(R.id.txtOrderListDishName);
            txtOrderListDishQuantity = (TextView) child.findViewById(R.id.txtOrderListDishQuantity);
            txtOrderListDishPrice = (TextView) child.findViewById(R.id.txtOrderListDishPrice);

            txtOrderListDishName.setText((String) stringObjectMap.get("txtOrderListDishName"));
            txtOrderListDishQuantity.setText((String) stringObjectMap.get("txtOrderListDishQuantity"));
            txtOrderListDishPrice.setText((String) stringObjectMap.get("txtOrderListDishPrice"));

            parent.addView(child);
        }

        return parent;


        // , , new String[]{"txtOrderListDishName", "txtOrderListDishQuantity", "txtOrderListDishPrice"}, new int[]{R.id.txtOrderListDishName, R.id.txtOrderListDishQuantity, R.id.txtOrderListDishPrice
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_order_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
